package com.situ.car.controller;

import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.entity.CarBrand;
import com.situ.car.service.ICarBrandService;
import com.situ.car.service.Impl.CarBrandServiceImpl;
import com.situ.car.util.MyBeanUtils;
import com.situ.car.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand")
public class CarBrandController extends BaseController{
    private ICarBrandService carBrandService = new CarBrandServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CarBrandController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectAll":
                selectAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarBrandController.add");
        CarBrand carBrand = MyBeanUtils.copyToBean(req, CarBrand.class);
        System.out.println(carBrand);
        carBrandService.add(carBrand);
        toJSON(resp, Result.ok("添加成功！"));
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarBrandController.selectAll");
        List<CarBrand> list = carBrandService.selectAll();
        toJSON(resp, Result.ok(list));
    }
}
